// 1.设置对应数量的搜索变量
var searchCarTimes = '';
var searchBMoney = '';
var searchEMoney = '';
var searchBStation = '';
var searchEStation = '';

// 2.设置分页变量
var start = 0; // 开始为止
var limit = 5; // 每页显示多少条
var end = 4; // 结束为止
var count = 0; // 一共多少条数据
var currPage = 1; // 当前第几页
var allPage = 0; // 一共多少页

function initTicket() {
	$.ajax({
		url: 'ticketPage',
		type: 'POST',
		data: {
			'searchCarTimes': searchCarTimes,
			'searchBMoney': searchBMoney,
			'searchEMoney': searchEMoney,
			'searchBStation': searchBStation,
			'searchEStation': searchEStation,
			'start': start,
			'limit': limit
		},
		dataType: 'JSON',
		async: true,
		success: function (resp) {
			console.log(resp);
			count = resp.datas.count;
			// 计算页码
			allPage = count % limit == 0 ? count / limit : parseInt(count / limit + 1);
			// 将页码显示到界面上
			var ticketPage = document.getElementById('ticketPage');
			ticketPage.innerHTML = currPage + "/" + allPage;

			var arr = resp.datas.ticket;
			ticketArr = arr;
			var dataBody = document.getElementById('dataBody');
			dataBody.innerHTML = '';
			for (var i = 0; i < arr.length; i++) {
				dataBody.innerHTML += `<tr>
					<td class="data-td" >
						<input class="selectItem" type="checkbox" />
					</td >
					<td class="data-td" >${arr[i].carTimes}</td>
					<td class="data-td" >${arr[i].beginStation}</td>
					<td class="data-td" >${arr[i].endStation}</td>
					<td class="data-td" >${getDate(arr[i].beginDatetime)}</td>
					<td class="data-td" >${getDate(arr[i].endDatetime)}</td>
					<td class="data-td" >${arr[i].ticketNum}</td>
					<td class="data-td" >${(arr[i].ticketMoney)}</td>
					<td class="data-td" >${arr[i].ticketState == 1 ? '启用': '禁用'}</td>
					<td class="data-td" >${getDate(arr[i].ticketCtime)}</td>
					<td class="data-td">
						<button class="btn" onclick="deleteTicket(${i})">删除</button>
						<button class="btn" onclick="showModifyFrame(${i})">修改</button>
					</td>
				</tr>`
			}
		},
		error: function (err) {
			alert('请联系管理员!');
		}
	})
}

initTicket();

function getDate(num) {
	if(!num) {
		return '解析错误';
	}
	var d = new Date(num);
	return d.getFullYear() + "-" +
		addZero(d.getMonth() + 1) + "-" +
		addZero(d.getDate()) + " " +
		addZero(d.getHours()) + ":" +
		addZero(d.getMinutes()) + ":" +
		addZero(d.getSeconds()) ;
}
// 补零
function addZero(data) {
	if(data > 9) {
		return data;
	}else {
		return "0" + data;
	}
}
//上一页
function doPre() {
	if(currPage == 1) {
		alert('已经是首页了!');
	}else {
		currPage --;
		start -= limit;
		end -= limit;
		initTicket();
	}
}
//下一页
function doNext() {
	if(currPage == allPage) {
		alert('已经是尾页了!');
	}else {
		currPage ++;
		start += limit;
		end += limit;
		initTicket();
	}
}
//搜索
function doSearch() {
	// 将所有的分页变量全部初始化
	start = 0; // 开始为止
	limit = 5; // 每页显示多少条
	end = 4; // 结束为止
	count = 0; // 一共多少条数据
	currPage = 1; // 当前第几页
	allPage = 0; // 一共多少页
	// 获取搜索框的值，赋值给搜索变量
	searchCarTimes = document.getElementById('sname').value;
	searchBMoney = document.getElementById('bmoney').value;
	searchEMoney = document.getElementById('emoney').value;
	searchBStation = document.getElementById('bstation').value;
	searchEStation = document.getElementById('estation').value;
	// 调用初始化方法
	initTicket();
}







//展示新增页面
function showAddFrame(){
	var frame = document.getElementById('addFrame');
	frame.style.display = "flex";
}
//隐藏新增页面
function hideAddFrame(){
	var frame = document.getElementById('addFrame');
	frame.style.display = "none";
}

//保存数据并隐藏新增面板
function saveAddFrame(){
	//修改时间格式
	var beginTime = document.getElementById('beginTime').value;
	var endTime = document.getElementById('endTime').value;
	var BDateTime = beginTime.replace(/T/," ");//将T替换成""
	var EDateTime = endTime.replace(/T/," ");
	$.ajax({
		url:'AddTicket',
		type: 'POST',
		async: true,
		data: {
			cartimes :  document.getElementById('carTimes').value,
			BStation: document.getElementById('beginStation').value,
			EStation: document.getElementById('endStation').value,
			BDateTime: BDateTime,
			EDateTime: EDateTime,
			ticketNum: document.getElementById('ticketNum').value,
			ticketMoney: document.getElementById('ticketMoney').value,
		},
		dataType:'JSON',
		success: function (resp) {
			console.log(resp);
			alert(resp.message);
			if(resp.id > 0) {
				doSearch();
				hideAddFrame();
			}
		},
		error: function (err) {
			alert('请联系管理员saveAddFrame')
		}
	})
}

//删除
function deleteTicket(index){
	console.log(index);
	var dt = confirm("确定删除?");
	if (dt == true) {
		$.ajax({
			url:'DeleteTicket',
			type: 'POST',
			async: true,
			data: {
				id : ticketArr[index].ticketId
			},
			dataType:'JSON',
			success: function (resp) {
				console.log(resp);
				alert(resp.message);
				if(resp.id > 0) {
					doSearch();
				}
			},
			error: function (err) {
				alert('请联系管理员deleteAdm')
			}
		})
	}
}
//展示修改面板
function showModifyFrame(index){
	console.log(index);
	document.getElementById('modifyFrame').style.display = "flex";

	var beginTime = ticketArr[index].beginDatetime.replace(/ /,"T");
	var endTime = ticketArr[index].endDatetime.replace(/ /,"T");
	// 将数据添加到界面上
	document.getElementById('modifyCarTimes').value = ticketArr[index].carTimes;
	document.getElementById('modifyBeginStation').value = ticketArr[index].beginStation;
	document.getElementById('modifyEndStation').value = ticketArr[index].endStation;
	document.getElementById('modifyBeginTime').value = beginTime;
	document.getElementById('modifyEndTime').value = endTime;
	document.getElementById('modifyTicketNum').value = ticketArr[index].ticketNum;
	document.getElementById('modifyTicketMoney').value = ticketArr[index].ticketMoney;
	document.getElementById('modifyTicketState').value = ticketArr[index].ticketState == 1 ? '启用': '禁用';
	ticketIndex = index;
}
//隐藏修改面板
function hideModifyFrame(){
	var modifyFrame = document.getElementById('modifyFrame');
	modifyFrame.style.display = 'none';
}
//保存修改面板
function savaModifyFrame(){
	//修改时间格式
	var modifyBeginTime = document.getElementById('modifyBeginTime').value;
	var modifyEndTime = document.getElementById('modifyEndTime').value;
	var BDateTime = modifyBeginTime.replace(/T/," ");//将T替换成""
	var EDateTime = modifyEndTime.replace(/T/," ");//将T替换成""
	//修改状态格式
	var state;
	var state1 = document.getElementById('modifyTicketState').value;
	if (state1=='启用'){
		state=1;
	}else {
		state=2;
	}
	$.ajax({
		url:'editTicketById',
		type: 'POST',
		async: true,
		data: {
			cartimes :  document.getElementById('modifyCarTimes').value,
			BStation: document.getElementById('modifyBeginStation').value,
			EStation: document.getElementById('modifyEndStation').value,
			BDateTime: BDateTime,
			EDateTime: EDateTime,
			ticketNum: document.getElementById('modifyTicketNum').value,
			ticketMoney: document.getElementById('modifyTicketMoney').value,
			state: state,
			id : ticketArr[ticketIndex].ticketId
		},
		dataType:'JSON',
		success: function (resp) {
			console.log(resp);
			alert(resp.message);
			if(resp.id > 0) {
				doSearch();
				hideModifyFrame();
			}
		},
		error: function (err) {
			alert('请联系管理员saveModifyFrame')
		}
	})
}
//全选按钮的监听
function selectAll(obj){
	var arr = document.getElementsByClassName('selectItem');
	for (var selectItem of arr) {
		selectItem.checked = obj.checked;
	}
}
//删除勾选的项
function removeSelect() {
	var arr = document.getElementsByClassName('selectItem');
	var arr1=[];
	var dt = confirm("确定删除?");
	if (dt == true) {
		//遍历选择的项
		for (var i = arr.length - 1; i >= 0; i--) {
			if (arr[i].checked == true) {
				// arr1.push(i);
				console.log(i);
				arr1.push(ticketArr[i].ticketId);

			}
			// }
		}
		$.ajax({
			url: 'DeleteTicket1',
			type: 'POST',
			async: true,
			data: {
				id: arr1
			},
			dataType: 'JSON',
			success: function (resp) {
				console.log(resp);
				alert(resp.message);
				if (resp.id > 0) {
					doSearch();
				}
			},
			error: function (err) {
				alert('请联系管理员removeSelect')
			}
		})
	}
}
